#define _CRT_SECURE_NO_WARNINGS 1
#include"func.h"

//暴力法
int searchInsert(int* nums, int numsSize, int target) {
    int i = 0;
    for (i = 0; i < numsSize; i++) {
        if (target <= nums[i])
            return i;
    }
    return numsSize;
}
//二分法
int searchInsert(int* nums, int numsSize, int target) {
    int left = 0;
    int right = numsSize - 1;
    while (left <= right) {
        int mid = left + (right - left);
        if (nums[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return left;
}

int romanToInt(char* s) {
    int symbolValues[26];
    symbolValues['I' - 'A'] = 1;
    symbolValues['V' - 'A'] = 5;
    symbolValues['X' - 'A'] = 10;
    symbolValues['L' - 'A'] = 50;
    symbolValues['C' - 'A'] = 100;
    symbolValues['D' - 'A'] = 500;
    symbolValues['M' - 'A'] = 1000;
    int n = strlen(s);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int value = symbolValues[s[i] - 'A'];
        if (i < n - 1 && value < symbolValues[s[i + 1] - 'A'])
            sum -= value;
        else
            sum += value;
    }
    return sum;
}